<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="供应商名称" prop="dsName">
        <el-input
          v-model="queryParams.dsName"
          placeholder="请输入供应商名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="dsContactName">
        <el-input
          v-model="queryParams.dsContactName"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商电话" prop="dsContactPhone">
        <el-input
          v-model="queryParams.dsContactPhone"
          placeholder="请输入供应商电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="dsState">
        <el-select v-model="queryParams.dsState" placeholder="供应商状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['drug:supplier:addSupplier']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['drug:supplier:updateSupplier']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['drug:supplier:deleteSupplier']"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="supplierList" @selection-change="handleSelectionChange" border="0px">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="供应商ID" align="center" prop="dsid" />
      <el-table-column label="供应商名称" align="center" prop="dsName" />
      <el-table-column label="联系人" align="center" prop="dsContactName" />
      <el-table-column label="联系人手机" align="center" prop="dsContactPhone" />
      <el-table-column label="银行账号" align="center" prop="dsBankCardId" />
      <el-table-column label="地址" align="center" prop="dsSite" />
      <el-table-column label="状态" align="center" prop="dsState" :formatter="statusFormat" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['drug:supplier:updateSupplier']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['drug:supplier:deleteSupplier']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="供应商名称" prop="dsName">
          <el-input v-model="form.dsName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="dsContactName">
          <el-input v-model="form.dsContactName" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="手机号" prop="dsContactPhone">
          <el-input v-model="form.dsContactPhone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="银行账号" prop="dsBankCardId">
          <el-input v-model="form.dsBankCardId" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="地址" prop="dsSite">
          <el-input v-model="form.dsSite" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="状态" prop="dsState">
          <el-radio-group v-model="form.dsState">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {selectAll,addSupplier,selectOne,updateSupplier,deleteSupplier} from "@/api/drug/supplier";

  export default {
    name: "supplier",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 供应商表格数据
        supplierList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态数据字典
        statusOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          dsName: undefined,
          dsContactName: undefined,
          dsContactPhone: undefined,
          dsState: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          dsName: [
            { required: true, message: "供应商名称不能为空", trigger: "blur" }
          ],
          dsContactName: [
            { required: true, message: "联系人不能为空", trigger: "blur" }
          ],
          dsContactPhone: [
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              required: true,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ],
          dsBankCardId: [
            {
              pattern: /^([1-9]{1})(\d{14}|\d{18})$/,
              required: true,
              message: "请输入正确的银行账号",
              trigger: "blur"
            }
          ],
          dsSite: [
            { required: true, message: "地址不能为空", trigger: "blur" }
          ]
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_normal_disable").then(response => {
        this.statusOptions = response.data;
      });
    },
    methods: {
      /** 查询生产厂家列表 */
      getList() {
        this.loading = true;
        selectAll(this.queryParams).then(response => {
          this.supplierList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 生产厂家状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.dsState);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          dsid: undefined,
          dsName: undefined,
          dsContactName: undefined,
          dsContactPhone: undefined,
          dsBankCardId: undefined,
          dsSite: undefined,
          dsState: "0",
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.dsid)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加供应商";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const dsid = row.dsid || this.ids
        selectOne(dsid).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改供应商";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.dsid != undefined) {
              updateSupplier(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addSupplier(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const dsids = row.dsid || this.ids;
        this.$confirm('是否确认删除生产厂家编号为"' + dsids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return deleteSupplier(dsids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      }
    }
  };
</script>
